/**
 * @param {object} obj  要转换的对象
 * @return {array} modules  转换后的数组
 */
module.exports.objectToArray = function(obj) {
  const keys = Object.keys(obj);
  const length = keys.length;
  const modules = [];
  for (let i = 0; i < length; i++) {
    modules.push(obj[keys[i]]);
  }
  return modules;
};

/**
 * 获取选择器指定元素的top值
 * @param {string} selector  微信css选择器
 * @return {*}
 */
module.exports.getElTop = function(selector) {
  return new Promise((resolve, reject) => {
    try {
      const query = wx.createSelectorQuery();
      query
          .select(selector)
          .boundingClientRect(function(rect) {})
          .exec(function(res) {
            resolve({top: res[0].top});
          });
    } catch (e) {
      reject(e);
    }
  });
};

/**
 * 判断指定元素是否达到顶部
 * 不怎么好用
 * @param {string} selector  微信css选择器
 * @return {boolean} result
 */
module.exports.isElArriveTop = function(selector) {
  return new Promise((resolve, rekect) => {
    try {
      const query = wx.createSelectorQuery();
      query
          .select(selector)
          .boundingClientRect(function(rect) {})
          .exec(function(res) {
            resolve(res[0].top === 0);
          });
    } catch (e) {
      reject(e);
    }
  });
};
